package solutions.leetcode.normal

import solutions.lib.ListNode

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/remove-nth-node-from-end-of-list/">19. 删除链表的倒数第 N 个结点</a>
 */
class Solution19 {
    fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
        var size = 0
        var p = head
        while (p != null) {
            size++
            p = p.next
        }
        var offset = size - n
        val root = ListNode()
        root.next = head
        p = root
        while (offset > 0) {
            p = p?.next
            offset--
        }
        p?.next = p?.next?.next
        return root.next
    }
}

fun main() {
    
}